package com.ygqh.baby.controller.admin;

import java.util.Date;
import java.util.Map;

import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ygqh.baby.ao.KeyWordsStatus;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.Message.Type;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.po.YgGroup;
import com.ygqh.baby.po.YgProduct;
import com.ygqh.baby.po.YgProductPack;
import com.ygqh.baby.po.YgSearchKeywords;
import com.ygqh.baby.service.YgSearchKeywordsService;
import com.ygqh.baby.utils.JsonUtils;

@Controller
@RequestMapping("/admin/keywords")
public class KeyWordsController extends BaseController<Object, java.lang.Long>{
	
	@Autowired
	private YgSearchKeywordsService ygSearchKeywordsService;
	
	/**
	 * 关键词后台查询
	 * @param q 查询条件
	 * @param queryInfo(分页)
	 * @return
	 */
	@RequestMapping("/searchKeyWords")
	@ResponseBody
	public Map<String,Object> searchKeywords(String q,QueryInfo queryInfo){
		
		ResultSet<YgSearchKeywords> resultSet = ygSearchKeywordsService.search(queryInfo,q);
		
		
		return resultSet.toDatatables(null);
	}
	
	
	/**
	 * 保存更改(根据ID是否为空判断修改还是保存)
	 * @param keywords
	 * @return
	 */
	@RequestMapping("/saveSearchKeywords")
	@ResponseBody
	public Message saveSearchKeywords(String sosoKeywords){
		
		int count=ygSearchKeywordsService.findByCount();
		if(count >= 25){
			return Message.error("推荐词输入已经达到25个上限！");
		}
		
		YgSearchKeywords searchKeywords = JsonUtils.toObject(sosoKeywords, YgSearchKeywords.class);
		
		searchKeywords.setCreateBy(getSession().getTrueName());
		searchKeywords.setCreateTime(new Date());
		
		
		return ygSearchKeywordsService.save(searchKeywords)==1?SUCCESS_MESSAGE:ERROR_MESSAGE;
	}
	
	/**
	 * 保存更改(根据ID是否为空判断修改还是保存)
	 * @param keywords
	 * @return
	 */
	@RequestMapping("/updateSearchKeywordsSort")
	@ResponseBody
	public Message updateSearchKeywordsSort(String sosoKeywords){
		YgSearchKeywords searchKeywords = JsonUtils.toObject(sosoKeywords, YgSearchKeywords.class);
		
		searchKeywords.setOperateBy(getSession().getTrueName());
		searchKeywords.setUpdateTime(new Date());
		
		return ygSearchKeywordsService.update(searchKeywords)==1?SUCCESS_MESSAGE:ERROR_MESSAGE;
	}
	
	@RequestMapping("/updateDefaultStatus")
	@ResponseBody
	public Message updateDefaultStatus(Long id){
		
		YgSearchKeywords ygSearchKeywords =new YgSearchKeywords();
		ygSearchKeywords.setStatus(KeyWordsStatus.show);
		
		ygSearchKeywordsService.updateByExampleSelectiveStatus(ygSearchKeywords);
		
		ygSearchKeywords.setStatus(KeyWordsStatus.defaul);
		ygSearchKeywords.setOperateBy(getSession().getTrueName());
		ygSearchKeywords.setUpdateTime(new Date());
		ygSearchKeywords.setId(id);
		return ygSearchKeywordsService.update(ygSearchKeywords)==1?SUCCESS_MESSAGE:ERROR_MESSAGE;
	}
	
	
	@RequestMapping("/deleteHideStatus")
	@ResponseBody
	public Message deleteSearchKeywords(Long id){
		
		YgSearchKeywords ygSearchKeywords =new YgSearchKeywords();
		ygSearchKeywords.setId(id);
		ygSearchKeywords.setOperateBy(getSession().getTrueName());
		ygSearchKeywords.setUpdateTime(new Date());
		ygSearchKeywords.setStatus(KeyWordsStatus.hide);
		
		return ygSearchKeywordsService.update(ygSearchKeywords)==1?SUCCESS_MESSAGE:ERROR_MESSAGE;
	}
	


	
	

	
	
}
